% This file provides a \begin{maple}...\end{maple} environment.  The
% environment is similar to \begin{verbatim}...\end{verbatim} in
% effect.  However there are several important differences.
%
% First of all, the spacing is quite different with respect to the
% surrounding environment.  This environment attempts not to introduce
% vertical white space at the beginning and end.
%
% Secondly the font is \tentt.  This environment is intended to be set
% in a surrounding 11pt or 12pt text. 
%
% Thirdly the environment is indented.
%
% Finally this environment formats its contents line by line,
% attempting to cause breaks to occur only at respectable points
% without causing grotesquely underfull/overfull pages.
%
% Written: Fall '89 by david Casperson
% Revised: 21 Jun 1990 by david Casperson to correct a ligature
%   problem in the maple environment discovered by Lee Qiao.
%   Hopefully !` and ?` now print correctly. 

% The next is a change (introduced by Silvio Levy) to make it work with 
% both old and new LaTeX -- Paulo Ney

\@ifundefined{extract@font}{}{\def\tentt{\small\tt\normalshape}} 

\makeatletter

\newlength{\mapleindent}      %% Indent of the left margin w.r.t. the
\setlength{\mapleindent}{1em} %% surrounding environment.


\newenvironment{maple}{%
% \tracingpages=1              %%%%%%%%% DEBUGGING %%%%%%%%
  \skip0=-1.0\parskip  %% Save to remove from end of environment
  \endgraf\parskip\z@  %% Note that we don't get the old \parskip at
                       %% the beginning of the environment.
  \@setsize\normalsize{10pt}\xpt\@xpt %% Make the lines very tight.
  %%\@setsize\normalsize{10.5pt}\xpt\@xpt %% Make the lines very tight.
                                        %% Helps simulate the glass
                                        %% tty look.
  \leftskip\@totalleftmargin\advance\leftskip\mapleindent
  \parindent\z@\parfillskip\@flushglue
  \tentt\frenchspacing 
  \vskip 0.5\baselineskip plus 2pt\relax%
  \def\do##1{\catcode `##1=12}\dospecials\@vobeyspaces%
  \catcode``=\active \@noligs \catcode`\^^M=12 
  \InOutputfalse \skiptokenafter\ttlines %% Steal the first linefeed token
}{\par\ifInOutput\mapleseparator\fi
%% We would like to break here, provided that we are close to the end
%% of the page.
  \vskip 0in plus 0.7in 
  \penalty -100
  \vskip 0in plus -0.7in
  \vskip\skip0 %% Compensate for the upcoming \parskip glue, provided
               %% that we are still on the same page.
  \@endpetrue}

% I don't know what gave me the brain damaged idea of simulating a
% verbatim environment by setting the \catcode of CR to other, but
% thats how this environment works.  
%
% \ttlines reads an argument delimited by a CR (other), i.e., one line
% of text, which it sends to \ttline for processing.  \ttline
% recognizes three different types of lines
%   a) blanks lines   (Recognizing these allows the maple environment
%                      to insert penalties that encourage page
%                      breaking at aesthetically pleasing points.)
%   b> \end{maple} lines.  (These terminate the maple environment)
%   c) all others.

\newif\ifMC            %% True if the current  line begins with >
\newif\ifInOutput      %% True if the previous line did not begin with >
\newif\ifttparwaiting  %% True if the previous line was blank

\begingroup \catcode `\^^M=12\relax%
\gdef\ttlines #1
   {\def\nextline{#1}\ttline}%
\endgroup

\begingroup \catcode `|=0 \catcode `[= 1
\catcode`]=2 \catcode `\{=12 \catcode `\}=12
\catcode`\\=12 |gdef|endmapleline[\end{maple}]
|endgroup

\def\ttline{\ifx\nextline\endmapleline \def\next{\end{maple}}%
             \else\if a\nextline a     \def\next{\ttblankline\ttlines}%
             \else                     \edef\next{\noexpand\printline
                                                  {\nextline}
                                                  \noexpand\ttlines}
             \fi\fi\next}



%  The following macro is called by \ttline to print one non-blank line.
%  It firsts prints a blank line if there is one pending.  It also
%  sets \ifMC to \MCtrue or \MCfalse depending on whether or not the
%  line begins with a > character.  This affects the interline
%  penalties inserted.
\def\printline#1{\ifttparwaiting\doblankline\fi
                 \futurelet\firstchar\setMC #1\mapleendline}
%
\def\setMC{\if>\firstchar\MCtrue\else\MCfalse\fi}
%
\def\mapleendline{\ifInOutput\ifMC\mapleseparator\fi\fi
               \par
               \ifMC \penalty 300\InOutputfalse
                  \else\penalty 10000\InOutputtrue \fi}

% The following macro is a hook to allow for MacIntosh style output separators.
\def\mapleseparator{%\nopagebreak\vskip 2pt plus 2pt\nopagebreak
                    %\hrule height 0.25 pt depth 0pt  %% if you want separators
                    \vskip 1pt plus 2pt\relax}

%% The following allows one to swallow a nuisance token.
\def\skiptokenafter#1#2{#1} 

%
%  The following two macros allow for the delayed processing of blank
%  lines within the maple environment.  The reason for this is that
%  most maple environments end with a blank line which ought not to be
%  translated into vertical white space.
%
%  \doblankline inserts a possible break point in order to allow large
%  output expressions break across pages if necessary.
%
\def\doblankline{\par\vskip 0pt plus 0.7in\penalty 300 \vskip 0pt plus -0.7in
           \vskip \baselineskip\ttparwaitingfalse}

\def\ttblankline{\ifttparwaiting\doblankline\fi
           \ttparwaitingtrue}

